FROM centos:7

COPY fusionpbx /var/www/fusionpbx
COPY fusionpbx-config.php /etc/fusionpbx/config.php
COPY nginx/fusionpbx /etc/nginx/sites-available/fusionpbx.conf

RUN set -ex; \
    groupadd -r -g 798 freeswitch; \
    useradd -r -g daemon -m -d /home/freeswitch -s /bin/bash -c "FusionPBX" -u 798 freeswitch;

RUN set -ex; \
    # Installing basics packages
    yum -y install yum-utils epel-release openssl; \
    #fusionpbx.sh
    yum -y install ghostscript libtiff-tools at; \
    #forensics tools
    yum -y install https://forensics.cert.org/cert-forensics-tools-release-el7.rpm; \
    yum -y --enablerepo=forensics install lame; \
    mkdir -p /var/cache/fusionpbx; \ 
    chown -R freeswitch:daemon /var/cache/fusionpbx; \
    mkdir -p /etc/fusionpbx; \
    chown -R freeswitch:daemon /etc/fusionpbx; \
    #send a message
    echo "FusionPBX Installed"; \
    #postgresql.sh
    #sslcert.sh
    # no default SSL in centos, generate a tmp certificate
    # ssl_certificate         /etc/ssl/certs/nginx.crt;
    # ssl_certificate_key     /etc/ssl/private/nginx.key;
    DOMAIN=$(hostname); \
    SSL_DIR="/etc/ssl"; \
    SUBJ="/C=US/ST=Idaho/O=FusionPBX/localityName=Boise/commonName=/organizationUnitName=/emailAddress=/"; \
    mkdir -p $SSL_DIR/private && mkdir -p $SSL_DIR/certs; \
    chmod 700 $SSL_DIR/private; \
    openssl req -x509 -nodes -subj "$SUBJ" -days 365 -newkey rsa:2048 -keyout "$SSL_DIR/private/nginx.key" -out "$SSL_DIR/certs/nginx.crt"; \
    #nginx.sh
    #send a message
    echo "Installing the web server"; \
    #install dependencies
    yum -y install nginx; \
    #setup nginx
    mkdir -p /etc/nginx/sites-available; \
    mkdir -p /etc/nginx/sites-enabled; \
    #enable fusionpbx nginx config; \
    ln -s /etc/nginx/sites-available/fusionpbx.conf /etc/nginx/sites-enabled/fusionpbx.conf; \
    awk '/server *{/ {c=1 ; next} c && /{/{c++} c && /}/{c--;next} !c' /etc/nginx/nginx.conf > /etc/nginx/nginx.tmp && mv -f /etc/nginx/nginx.tmp /etc/nginx/nginx.conf && rm -f /etc/nginx/nginx.tmp; \
    sed -i '/include \/etc\/nginx\/conf\.d\/\*\.conf\;/a \    include \/etc\/nginx\/sites-enabled\/\*\.conf\;' /etc/nginx/nginx.conf; \
    #set the log permissions
    chmod -R 664 /var/log/nginx/; \
    #send a message
    echo "nginx installed"; \
    #php.sh
    #send a message
    echo "Install PHP and PHP-FPM"; \
    # Add additional repository
    yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm; \
    yum-config-manager --enable remi-php72;\
    #install dependencies
    yum -y install php-fpm php-gd php-pgsql php-odbc php-curl php-imap php-opcache php-common php-pdo php-soap php-xml php-xmlrpc php-cli php-gd; \
    sed -i 's/user nginx/user freeswitch daemon/g' /etc/nginx/nginx.conf; \
    chown -Rf freeswitch:daemon /var/lib/nginx; \
    sed -ie 's/user = apache/user = freeswitch/g' /etc/php-fpm.d/www.conf; \
    chown -R freeswitch:daemon /var/lib/php/session; \
    #send a message
    echo "Configuring php/nginx/php-fpm and permissions"; \
    #get the timezone
    TIMEZ=America/New_York; \
    #update the php configuration
    sed -ie "s#;date.timezone =#date.timezone = $TIMEZ#g" /etc/php.ini; \
    sed -ie 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php.ini; \
    sed -ie "s|listen = 127.0.0.1:9000|listen = /var/run/php-fpm/php-fpm.sock|g" /etc/php-fpm.d/www.conf; \
    sed -ie 's/;listen.owner = nobody/listen.owner = freeswitch/g' /etc/php-fpm.d/www.conf; \
    sed -ie 's/;listen.group = nobody/listen.group = daemon/g' /etc/php-fpm.d/www.conf; \
    sed -ie 's/group = apache/group = daemon/g' /etc/php-fpm.d/www.conf; \
    #make the session directory
    mkdir -p /var/lib/php/session; \
    mkdir /var/run/php; \
    #update permissions
    chmod -Rf 770 /var/lib/php/session; \
    #send a message
    echo "php/nginx/php-fpm and permissions configured"; \
    #switch.sh -> package-release.sh
    #install freeswitch packages
    echo "carpe" > /etc/yum/vars/signalwireusername; \
    echo "pat_o5VYvTGAJUUDMpxCYkfhaU36" > /etc/yum/vars/signalwiretoken; \
    yum install -y https://$(< /etc/yum/vars/signalwireusername):$(< /etc/yum/vars/signalwiretoken)@freeswitch.signalwire.com/repo/yum/centos-release/freeswitch-release-repo-0-1.noarch.rpm epel-release; \
    yum install -y freeswitch-config-vanilla freeswitch-lang-* freeswitch-sounds-en-* freeswitch-sounds-music* freeswitch-lua freeswitch-xml-cdr; \
    yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm;\
    yum install -y postgresql14-contrib postgresql14 postgresql14-libs supervisor; \
    rm -rf /var/cache;

COPY entrypoint.sh /usr/bin/entrypoint.sh
COPY supervisor/supervisord.conf /etc/supervisord.conf
COPY supervisor/supervisord.d /etc/supervisord.d
RUN  set -ex; \
    chmod 555 /usr/bin/entrypoint.sh; \
    mkdir /var/run/php-fpm; \
    chown -R freeswitch:daemon /var/run/php-fpm; \
    chown -R freeswitch:daemon /var/log/php-fpm;
RUN set -ex; \
    #copy the conf directory
    mv /etc/freeswitch /etc/freeswitch.orig; \
    mkdir /etc/freeswitch; \
    cp -R /var/www/fusionpbx/resources/templates/conf/* /etc/freeswitch;

ENTRYPOINT [ "/usr/bin/entrypoint.sh" ]
CMD [ "/usr/bin/supervisord", "-c", "/etc/supervisord.conf" ]